﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://wpfimageviewer.codeplex.com
using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using ImageAppDataModels;
using System.Collections.Generic;
using System.IO;
using BrainTechLLC;

namespace DirectoryViewModels
{
    public class DirectoriesToExamineCollectionViewModel : ObservableCollection<DirectoriesToExamineViewModel>
    {
        public event EventHandler<ObjectEventArgs<DirectoriesToExamineCollectionViewModel>> CurrentlySelectedGroupsInListboxChanged;
        protected DirectoriesToExamineCollection _dirsToExamine;
        protected bool _ShowOnlySelectedGroups;
        protected List<DirectoriesToExamineViewModel> _CurrentlySelectedGroupsInListbox = new List<DirectoriesToExamineViewModel>();

        public bool ShowOnlySelectedGroups
        {
            get { return _ShowOnlySelectedGroups; }
            set
            {
                if (_ShowOnlySelectedGroups != value)
                {
                    _ShowOnlySelectedGroups = value;
                    OnPropertyChanged(new System.ComponentModel.PropertyChangedEventArgs("ShowOnlySelectedGroups"));
                }
            }
        }

        public List<DirectoriesToExamineViewModel> CurrentlySelectedGroupsInListbox
        {
            get { return _CurrentlySelectedGroupsInListbox; }
        }

        public void Refresh()
        {
            OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
        }

        public DirectoriesToExamineCollectionViewModel() { _dirsToExamine = new DirectoriesToExamineCollection(); }
        public DirectoriesToExamineCollectionViewModel(DirectoriesToExamine dirs)
        {
            _dirsToExamine = new DirectoriesToExamineCollection();
            Add(dirs);
        }

        public void Add(DirectoriesToExamine dirs)
        {
            DirectoriesToExamineViewModel vm = new DirectoriesToExamineViewModel(dirs);
            _dirsToExamine.Add(dirs);
            base.Add(vm);
        }

        public void Save(string pathToExaminePath)
        {
            _dirsToExamine.Save(pathToExaminePath);
        }

        public static DirectoriesToExamineCollectionViewModel Load(string pathToExaminePath, ICoreDataProvider dataProvider)
        {
            DirectoriesToExamineCollectionViewModel result = new DirectoriesToExamineCollectionViewModel();
            result._dirsToExamine.DataProvider = dataProvider;
            try
            {
                var dirs = DirectoriesToExamineCollection.Load(pathToExaminePath, dataProvider);
                foreach (var dir in dirs)
                {
                    result.Add(dir);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            return result;
        }

        public DirectoriesToExamineViewModel FindName(string name)
        {
            return this.FirstOrDefault(o => o.Name != null && o.Name.Equals(name, StringComparison.OrdinalIgnoreCase));
        }

        public void AddOrFindGroup(string groupName)
        {
            DirectoriesToExamine dirs = new DirectoriesToExamine();
            dirs.Name = groupName;
            this.Add(dirs);
        }

        public void RemoveGroup(DirectoriesToExamineViewModel vm)
        {
            for (int n = _dirsToExamine.Count - 1; n >= 0; n--)
            {
                if (_dirsToExamine[n].Name == vm.Name)
                {
                    _dirsToExamine.RemoveAt(n);
                }
            }

            this.Remove(vm);
        }

        public void OnCurrentlySelectedGroupsInListboxChanged()
        {
            CurrentlySelectedGroupsInListboxChanged.RaiseEvent(this, this);
        }
    }
}
